﻿using System;
using System.IO;
using System.Resources;

namespace CreateResx
{
	class Program
	{
		const string CSHARP_SQL_UTILITY_FUNCTIONS = @"using System;
using System.Diagnostics;

namespace SqlUtilityFunctions
{
	class ConfiguredSettings
	{
		static public TimeSpan LdapClientTimeout { get { return new TimeSpan(0, 0, 1200); } }

		static public int TraceLevel { get { return 1; } }
	}

	class SharedLogic
	{
		static public void LogTraceInfo(TraceListener listenerToUse, Exception ex, TraceLevel messageLevel)
		{
			if (!Trace.Listeners.Contains(listenerToUse))
			{
				Trace.Listeners.Add(listenerToUse);
			}
			switch (messageLevel)
			{
				case TraceLevel.Error:
					Trace.TraceError(""Exception Type:\t{ 0}\nError Message:\t{ 1}\nStack:\t{ 2}""
						, ex.GetType().ToString()
						, ex.Message
						, ex.StackTrace);
				break;
			}
			Trace.Listeners.Remove(listenerToUse);
		}
	}
}";

		static void Main(string[] args)
		{
			//edit the resx file for the DynamicallyBuildUDFBasedOnSchema project
			using (ResXResourceWriter w = new ResXResourceWriter(args[0]))
			{
				int count = 0;
				//Get the list of C# files from the directory Active Directory Utils is downloaded to.
				DirectoryInfo targetDir = new DirectoryInfo(args[1]);
				foreach (FileInfo fi in targetDir.GetFiles("*.cs",SearchOption.AllDirectories))
				{
					if (fi.Name.ToLowerInvariant() != "AssemblyInfo.cs".ToLowerInvariant() && fi.Name.ToLowerInvariant() != "Program.cs".ToLowerInvariant())
					{
						w.AddResource("CSharp_"
							+ fi.Directory.Name + "_"
							+ fi.Name.Substring(0, fi.Name.LastIndexOf('.')).Replace('.', '_').Replace('-', '_')
							+ (count++).ToString()
							, fi.OpenText().ReadToEnd());
					}
				}

				w.AddResource("CSharp_SqlUtilityFunctions",CSHARP_SQL_UTILITY_FUNCTIONS);
			}
		}
	}
}
